package com.example.demo12.dao;

import com.example.demo12.dbdatabase;
import com.example.demo12.entity.Product;

import java.sql.*;
import java.util.*;

public class ProductDAO {
    private dbdatabase db;
    private Connection connection;

    public ProductDAO() {
        connection=db.getConnection();
    }

    public List<Product> getAllProducts() throws SQLException {
        List<Product> products = new ArrayList<>();
        String sql = "SELECT * FROM products";
        try (Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery(sql)) {
            while (resultSet.next()) {
                Product product = new Product();
                product.setId(resultSet.getInt("id"));
                product.setName(resultSet.getString("name"));
                product.setPrice(resultSet.getDouble("price"));
                products.add(product);
            }
        }
        return products;
    }
}
